package org.linlinjava.litemall.admin.web;



import org.linlinjava.litemall.admin.service.AdminUserMemberService;
import org.linlinjava.litemall.core.core.core.domain.AjaxResult;
import org.linlinjava.litemall.core.core.core.domain.R;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.LitemallMemberTimes;
import org.linlinjava.litemall.db.domain.LitemallUser;
import org.linlinjava.litemall.db.domain.LitemallUserMember;
import org.linlinjava.litemall.db.domain.UserMemberFreeze;
import org.linlinjava.litemall.db.param.UserMemberFreezeParam;
import org.linlinjava.litemall.db.service.LitemallMemberTimesService;
import org.linlinjava.litemall.db.service.LitemallUserMemberService;
import org.linlinjava.litemall.db.service.LitemallUserService;
import org.linlinjava.litemall.db.service.UserMemberFreezeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author Administrator
 */
@RestController
@RequestMapping("/admin/userMember")
public class AdminUserMemberController {
    @Autowired
    private AdminUserMemberService adminUserMemberService;
    @Autowired
    private LitemallUserMemberService userMemberService;
    @Autowired
    private LitemallUserService userService;
    @Autowired
    private UserMemberFreezeService userMemberFreezeService;
    @Autowired
    private LitemallMemberTimesService litemallMemberTimesService;

    /**
     * 后台展示所有会员用户(可根据nickname模糊查询和ruleId精确查询)
     *
     * @param nickname
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @return
     */
    @GetMapping("/listAll")
    public Object listAll(String nickname,
                          @RequestParam(required = false, defaultValue = "1") Integer page,
                          @RequestParam(required = false, defaultValue = "10") Integer limit,
                          @RequestParam(required = false, defaultValue = "add_time") String sort,
                          @RequestParam(required = false, defaultValue = "desc") String order,
                          @RequestParam(required = false, defaultValue = "") String mobile,
                          @RequestParam(required = false, defaultValue = "") String[] effective
    )    {
        return adminUserMemberService.listAll(nickname, page, limit, sort, order,mobile,effective);

    }


    @GetMapping("/agencyList")
    public Object agencyList(
                          @RequestParam(required = false) Integer userId,
                          @RequestParam(required = false, defaultValue = "") String effective
    )    {
        return adminUserMemberService.list(userId,effective);

    }

    /**
     * 获取会员用户详情
     *
     * @param id
     * @return
     */
    @GetMapping("/detail")
    public Object getDetail(@RequestParam Integer id) {
        return ResponseUtil.ok(adminUserMemberService.getById(id));
    }

    /**
     * 后台手动修改会员天数
     * @param userMember
     * @return
     */
    @PostMapping("/update")
    public Object update(@RequestBody LitemallUserMember userMember) {
        userMemberService.updateById(userMember);
        LitemallUser user = userService.findById(userMember.getUserId());
        user.setMobile(userMember.getMobile());
        userService.updateById(user);
        return ResponseUtil.ok();
    }

    @GetMapping("/listDeleteAll")
    public Object listDeleteAll(String nickname,
                          @RequestParam(required = false, defaultValue = "1") Integer page,
                          @RequestParam(required = false, defaultValue = "10") Integer limit,
                          @RequestParam(required = false, defaultValue = "add_time") String sort,
                          @RequestParam(required = false, defaultValue = "desc") String order) {
        return adminUserMemberService.listDeleteAll(nickname, page, limit, sort, order);

    }


    @PostMapping("/freeze")
    public AjaxResult create(@RequestBody UserMemberFreezeParam param) {

        UserMemberFreeze userMemberFreeze = new UserMemberFreeze();
        BeanUtils.copyProperties(param, userMemberFreeze);
        userMemberFreeze.setState(param.getState());
        userMemberFreezeService.insert(userMemberFreeze);
        return AjaxResult.success();
    }


    @PostMapping("/updateTimes")
    public AjaxResult updateTimes(@RequestBody LitemallMemberTimes times) {
        litemallMemberTimesService.updateById(times);
        return AjaxResult.success();
    }
}
